<?php 
	namespace app\admin\controller;
	use think\Controller;
	use think\Db;
	//后台主页
	class users extends Allow
	{
		public function getIndex()
		{
			//获取users表的信息
			$list=Db::table('users')->select();
			//加载模板
			return $this->fetch('users/index',['list'=>$list]);
		}
		public function getEdit()
		{
			$request=request();
			//判断是否有ID
			if(!empty($request->param('id'))){
				$id=$request->param('id');
				//获取单条数据
				$list=Db::table('users')->where('id',$id)->find();
				//加载模板
			    return $this->fetch('users/edit',['list'=>$list]);

			}else{
				$this->error('没有相应的数据','/users/index');
				exit;
			}
			
		}
		public function postUpdate()
		{
			$request=request();
			//填写空值则返回
			if($_POST['name']=="")
			{
				$this->error('请填写完整','/users/index');
				exit;
			}else
			{
				$id=$request->param('id');
				$list=Db::table('users')->where('id',$id)->update($_POST);
				if($list)
				{
					//修改成功
					$this->success('修改成功','/users/index');
				}else
				{
					//修改失败
					$this->error('修改失败','/users/index');
				}
			}

		}
		public function getdelete()
		{
			$request=request();
			//获取ID
			$id=$request->param('id');
	        $list=Db::table('users')->where('id',$id)->delete();
	        if($list)
	        {
	        	//删除成功
	        	$this->success('删除成功','/users/index');
	        }else
	        {
	        	//删除失败
	        	$this->success('删除失败','/users/index');
	        }

		}
		public function getadd()
		{
			$data=Db::table('users')->select();
			return $this->fetch('users/add',['data'=>$data]);
		}
		public function postinsert()
		{
			$request=request();
			if($_POST['name']=="")
			{
				$this->error('请填写完整','/users/add');
				exit;
			}else
			{
				$list=Db::table('users')->insert($_POST);
				if($list)
				{
					$this->success('添加成功','/users/index');
				}else
				{
					$this->error('添加失败','/users/index');
				}
			}
		}
	}
 ?>